﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace VIP_Shop.Access_Data
{
    class AccessData
    {
        private DataTable table = new DataTable();
        private SqlConnection con = new SqlConnection();
        SqlDataAdapter adt = new SqlDataAdapter();
        SqlDataAdapter adt1 = new SqlDataAdapter();
        SqlDataAdapter adt2 = new SqlDataAdapter();
        public SqlConnection getConn
        {
            get { return con; }
        }

        public AccessData()
        {
            con.ConnectionString =@" Data Source=DARK\SQLEXPRESS;Initial Catalog=banhang;Integrated Security=True";
        }

        public void Open()
        {
            if (con.State == ConnectionState.Closed)
                con.Open();
        }

        public void Close()
        {
            if (con.State == ConnectionState.Open)
                con.Close();
        }

        public DataTable get_Datatable(String qr)
        {
            adt.SelectCommand = new SqlCommand(qr, con);
            adt.Fill(table);
            return table;
        }

        public DataTable get_Datatable1(String qr)
        {
            DataTable tb = new DataTable();
            adt1.SelectCommand = new SqlCommand(qr, con);
            adt1.Fill(tb);
            return tb;
        }
        public DataTable get_Datatable2(String qr)
        {
            DataTable tb = new DataTable();
            adt2.SelectCommand = new SqlCommand(qr, con);
            adt2.Fill(tb);
            return tb;
        }

        public DataSet get_Dataset(String qr)
        {
            DataTable tb = new DataTable();
            DataSet ds = new DataSet();
            ds.Tables.Add(tb);
            return ds;
        }

        public void Execute_SQL(String qr)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = qr;
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }

        public void Update_Dataset(ref DataTable tb, String qr)
        {
            SqlDataAdapter adt = new SqlDataAdapter(qr, con);
            SqlCommandBuilder bd = new SqlCommandBuilder();
            bd.DataAdapter = adt;
            adt.Update(table);
        }
//cap nhap datagridview vao csdl
        public void UpdateView(DataTable table)
        {
            SqlCommandBuilder db = new SqlCommandBuilder(adt);
            adt.Update(table);
        }
        public void UpdateView1(DataTable table)
        {
            SqlCommandBuilder db = new SqlCommandBuilder(adt1);
            adt1.Update(table);
        }

        public void Update(String qr)
        {
            SqlCommand cmd = new SqlCommand(qr, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            table.Clear();
            adt.Fill(table);
        }
        public object GetOneData(string strUpdate)
        {
            SqlCommand com = new SqlCommand(strUpdate, con);
            con.Open();
            object obj = com.ExecuteScalar();
            con.Close();
            return obj;
        }
        public  SqlDataReader ThucHienReader(string select)
        {
            SqlCommand cmd = new SqlCommand(select,con );
            try
            {
                return cmd.ExecuteReader();
            }
            catch (SqlException)
            {
                return null;
            }
        } 
    }
}
